#include <iostream>
#include <string>
#include <queue>
using namespace std;


struct people{
	string name;
	int y,m,d,i;
	bool operator < (const people x)const{
		if(y > x.y)
			return true;
		if(y < x.y)
			return false;
		if(m > x.m)
			return true;
		if(m < x.m)
			return false;
		if(d > x.d)
			return true;
		if(d < x.d)
			return false;
		if(i > x.i)
			return true;
		if(i < x.i)
			return false;
	}
};


int main(){
	int n;
	cin>>n;
	priority_queue<people> q;
	for(int i=0;i<n;i++){
		people l;
		cin>>l.name;
		cin>>l.y;
		cin>>l.m;
		cin>>l.d;
		l.i = i;
		q.push(l);
	}
	for(int i=0;i<n;i++){
		cout<<q.top().name<<endl;
		q.pop();
	}
	return 0;
}
